// pages/dish-admin/dish-admin.js
// dish-admin.js
const app = getApp();
Page({
    data: {
      categories: ['家常小炒🍳', '时令素菜🥬', '蒸煮鲜食🥘', '荤素炖菜🍲', '凉拌小菜🥗', '家常汤羹🥣', '小酒饮料🥤'],
      categoryIndex: 0,
      currentTab: 0,
      dishName: '',
      dishIngredients: '',
      preciousIngredients: '',
      customDishes: [],
      filteredDishes: []
    },
    
    onLoad: function() {
      // 加载自定义菜品
      this.loadCustomDishes();
    },
    
    // 加载自定义菜品
    loadCustomDishes: function() {
      const customDishes = wx.getStorageSync('customDishes') || [];
      this.setData({
        customDishes,
        filteredDishes: this.filterDishesByCategory(customDishes, 0)
      });
    },
    
    // 返回上一页
    goBack: function() {
      wx.navigateBack();
    },
    
    // 切换标签
    switchTab: function(e) {
      const index = e.currentTarget.dataset.index;
      this.setData({
        currentTab: index,
        filteredDishes: this.filterDishesByCategory(this.data.customDishes, index)
      });
    },
    
    // 根据分类筛选菜品
    filterDishesByCategory: function(dishes, tabIndex) {
      const category = this.data.categories[tabIndex];
      return dishes.filter(dish => dish.category === category);
    },
    
    // 输入处理
    onCategoryChange: function(e) {
      this.setData({
        categoryIndex: e.detail.value
      });
    },
    
    onDishNameInput: function(e) {
      this.setData({
        dishName: e.detail.value
      });
    },
    
    onIngredientsInput: function(e) {
      this.setData({
        dishIngredients: e.detail.value
      });
    },
    
    onPreciousInput: function(e) {
      this.setData({
        preciousIngredients: e.detail.value
      });
    },
    
    // 添加菜品
    addDish: function() {
      const { dishName, dishIngredients, preciousIngredients, categoryIndex, categories, customDishes } = this.data;
      
      if (!dishName.trim()) {
        wx.showToast({
          title: '请输入菜品名称',
          icon: 'none'
        });
        return;
      }
      
      if (!dishIngredients.trim()) {
        wx.showToast({
          title: '请输入主要材料',
          icon: 'none'
        });
        return;
      }
      
      // 创建新菜品
      const categoryMap = {
        '家常小炒🍳': 'chef',
        '时令素菜🥬': 'staple',
        '蒸煮鲜食🥘': 'stirfry',
        '荤素炖菜🍲': 'stew',
        '凉拌小菜🥗': 'cold',
        '家常汤羹🥣': 'soup',
        '小酒饮料🥤': 'drink'
      };
      
      const selectedCategory = categories[categoryIndex];
      const categoryId = categoryMap[selectedCategory];
      
      const newDish = {
        id: 'custom_' + Date.now(),
        name: dishName.trim(),
        ingredients: dishIngredients.trim(),
        precious: preciousIngredients.trim(),
        category: selectedCategory,
        categoryId: categoryId,
        isCustom: true
      };
      
      // 添加到列表
      customDishes.push(newDish);
      
      // 保存到本地存储
      wx.setStorageSync('customDishes', customDishes);
      
      // 更新列表
      this.setData({
        customDishes,
        filteredDishes: this.filterDishesByCategory(customDishes, this.data.currentTab),
        dishName: '',
        dishIngredients: '',
        preciousIngredients: ''
      });
      
      // 更新首页数据
      this.updateIndexPage();
      
      wx.showToast({
        title: '添加成功',
        icon: 'success'
      });
    },
    
    // 编辑菜品
    editDish: function(e) {
      const dishId = e.currentTarget.dataset.id;
      const dish = this.data.customDishes.find(item => item.id === dishId);
      
      if (dish) {
        // 在实际应用中，这里可以跳转到编辑页面或显示编辑模态框
        wx.showModal({
          title: '编辑菜品',
          content: '编辑功能尚未实现',
          showCancel: false
        });
      }
    },
    
    // 删除菜品
    deleteDish: function(e) {
      const dishId = e.currentTarget.dataset.id;
      const that = this;
      
      wx.showModal({
        title: '确认删除',
        content: '确定要删除这个菜品吗？',
        success(res) {
          if (res.confirm) {
            const customDishes = that.data.customDishes.filter(item => item.id !== dishId);
            
            // 保存到本地存储
            wx.setStorageSync('customDishes', customDishes);
            
            // 更新列表
            that.setData({
              customDishes,
              filteredDishes: that.filterDishesByCategory(customDishes, that.data.currentTab)
            });
            
            // 更新首页数据
            that.updateIndexPage();
            
            wx.showToast({
              title: '删除成功',
              icon: 'success'
            });
          }
        }
      });
    },
    
    // 更新首页数据
    updateIndexPage: function() {
      const pages = getCurrentPages();
      if (pages.length > 0) {
        const indexPage = pages[0];
        if (indexPage && indexPage.route === 'pages/index/index') {
          indexPage.loadCustomDishes && indexPage.loadCustomDishes();
        }
      }
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})